﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Web.Configuration;

public partial class admin_UpdatePoster : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username"] == null)
                Response.Redirect("Login.aspx");
            else
            {
                if (GetConfig("poster1") != "")
                    this.imgPoster1.ImageUrl = "." + GetConfig("poster1");
                else
                    this.imgPoster1.ImageUrl = "";

                if (GetConfig("poster2") != "")
                    this.imgPoster2.ImageUrl = "." + GetConfig("poster2");
                else
                    this.imgPoster2.ImageUrl = "";


                if (GetConfig("poster3") != "")
                    this.imgPoster3.ImageUrl = "." + GetConfig("poster3");
                else
                    this.imgPoster3.ImageUrl = "";

                if (GetConfig("poster4") != "")
                    this.imgPoster4.ImageUrl = "." + GetConfig("poster4");
                else
                    this.imgPoster4.ImageUrl = "";
            }
        }

    }
    protected void btnUpdate1_Click(object sender, ImageClickEventArgs e)
    {
        string StrImg = "";
        if (this.imgUpload1.HasFile)
        {
            try
            {
                StrImg = Path.GetFileName(imgUpload1.FileName);
                imgUpload1.SaveAs(Server.MapPath("../img/") + StrImg);
                //StatusLabel.Text = "Upload status: File uploaded!";
            }
            catch (Exception ex)
            {
                this.lblError.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                return;
            }
        }
        UpdateConfig("poster1", "./img/" + StrImg);
        this.imgPoster1.ImageUrl = "../img/" + StrImg;
        //this.Response.Redirect("UpdatePoster.aspx");
    }
    protected void btnUpdate2_Click(object sender, ImageClickEventArgs e)
    {
        string StrImg = "";
        if (this.imgUpload2.HasFile)
        {
            try
            {
                StrImg = Path.GetFileName(imgUpload2.FileName);
                imgUpload2.SaveAs(Server.MapPath("../img/") + StrImg);
                //StatusLabel.Text = "Upload status: File uploaded!";
            }
            catch (Exception ex)
            {
                this.lblError.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                return;
            }
        }
        UpdateConfig("poster2", "./img/" + StrImg);
        this.imgPoster2.ImageUrl = "../img/" + StrImg;
        //this.Response.Redirect("UpdatePoster.aspx");
    }
    protected void btnUpdate3_Click(object sender, ImageClickEventArgs e)
    {
        string StrImg = "";
        if (this.imgUpload3.HasFile)
        {
            try
            {
                StrImg = Path.GetFileName(imgUpload3.FileName);
                imgUpload3.SaveAs(Server.MapPath("../img/") + StrImg);
                //StatusLabel.Text = "Upload status: File uploaded!";
            }
            catch (Exception ex)
            {
                this.lblError.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                return;
            }
        }
        UpdateConfig("poster3", "./img/" + StrImg);
        this.imgPoster3.ImageUrl = "../img/" + StrImg;
        //this.Response.Redirect("UpdatePoster.aspx");
    }
    protected void btnUpdate4_Click(object sender, ImageClickEventArgs e)
    {
        string StrImg = "";
        if (this.imgUpload4.HasFile)
        {
            try
            {
                StrImg = Path.GetFileName(imgUpload4.FileName);
                imgUpload4.SaveAs(Server.MapPath("../img/") + StrImg);
                //StatusLabel.Text = "Upload status: File uploaded!";
            }
            catch (Exception ex)
            {
                this.lblError.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                return;
            }
        }
        UpdateConfig("poster4", "./img/" + StrImg);
        this.imgPoster4.ImageUrl = "../img/" + StrImg;
        //this.Response.Redirect("UpdatePoster.aspx");
    }

    private void UpdateConfig(string strKey, string strValue)
    {
        Configuration objConfig = WebConfigurationManager.OpenWebConfiguration("~");
        AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings");
        if (objAppsettings != null)
        {
            objAppsettings.Settings[strKey].Value = strValue;
            objConfig.Save();
        }
    }
    private void DeleteConfig(string strKey)
    {
        Configuration objConfig = WebConfigurationManager.OpenWebConfiguration("~");
        AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings");
        if (objAppsettings != null)
        {
            objAppsettings.Settings[strKey].Value = "";
            objConfig.Save();
        }
    }
    private string GetConfig(string strKey)
    {
        string str = ConfigurationManager.AppSettings.GetValues(strKey)[0];
        return str;
    }
    protected void btnDelete1_Click(object sender, ImageClickEventArgs e)
    {
        UpdateConfig("poster1", "");
        //Response.Redirect("UpdatePoster.aspx");
        this.imgPoster1.ImageUrl = "";
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        UpdateConfig("poster2", "");
        //Response.Redirect("UpdatePoster.aspx");
        this.imgPoster2.ImageUrl = "";
    }
    protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
    {
        UpdateConfig("poster3", "");
        //Response.Redirect("UpdatePoster.aspx");
        this.imgPoster3.ImageUrl = "";
    }
    protected void ImageButton6_Click(object sender, ImageClickEventArgs e)
    {
        UpdateConfig("poster4", "");
        //Response.Redirect("UpdatePoster.aspx");
        this.imgPoster4.ImageUrl = "";

    }
}
